package com.atguigu.sparkcore.rdd.tranformsinglevale

import com.atguigu.sparkcore.util.MySparkContextUtil
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
  * rdd转换-FlatMap
  * 将每个元素可以映射为0个或多个输出
  */
object FlatMap {

  def main(args: Array[String]): Unit = {

    // 获取sparkContext
    val sc = MySparkContextUtil.get(args)

    // 创建rdd
    val list: Array[Int] = Array(10, 20, 30, 40, 50, 60)
    val rdd: RDD[Int] = sc.makeRDD(list, 2)

    // 转换rdd
     val result: RDD[Int] = rdd.flatMap(x => Array(x * x,x*x*x))

    // 输出
    println(result.collect().mkString(","))

    // 关闭资源
    MySparkContextUtil.close(sc)
  }
}
